package 二叉树;

import jdk.nashorn.api.tree.Tree;

/**
 * 104. Maximum Depth of Binary Tree
 * https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
 *
 * Given a binary tree, find its maximum depth.
 *
 * The maximum depth is the number of nodes along
 * the longest path from the root node down to the farthest leaf node.
 *
 * Note: A leaf is a node with no children.
 *
 * Example:
 *
 * Given binary tree [3,9,20,null,null,15,7],
 *
 *     3
 *    / \
 *   9  20
 *     /  \
 *    15   7
 * return its depth = 3.
 *
 * 类似：
 * E_559_N叉树的最大深度
 *
 */
public class E_104_二叉树的最大深度 {

    /**
     * 递归解决
     * rootDepth = 1 + Max(root.leftDepth, root.rightDepth)
     * node == null 0
     * # node.left == null && node.right == null 1
     */
    public static int maxDepth(TreeNode root) {

        if (root == null) return 0;
//        if (root.left == null && root.right == null) return 1;

        return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        root.right.left.right = new TreeNode(6);

        System.out.println(maxDepth(root));
    }
}
